---
title: variant
description: Creates a variant schema.
source: /schemas/variant/variant.ts
contributors:
  - fabian-hiller
---

import { ApiList, Property } from '~/components';
import { properties } from './properties';

# variant

Creates a variant schema.

```ts
const Schema = v.variant<TKey, TOptions, TMessage>(key, options, message);
```

## Generics

- `TKey` <Property {...properties.TKey} />
- `TOptions` <Property {...properties.TOptions} />
- `TMessage` <Property {...properties.TMessage} />

## Parameters

- `key` <Property {...properties.key} />
- `options` <Property {...properties.options} />
- `message` <Property {...properties.message} />

### Explanation

With `variant` you can validate if the input matches one of the given object `options`. The object schema to be used for the validation is determined by the discriminator `key`. If the input does not match a schema and cannot be clearly assigned to one of the options, you can use `message` to customize the error message.

> It is allowed to specify the exact same or a similar discriminator multiple times. However, in such cases `variant` will only return the output of the first untyped or typed variant option result. Typed results take precedence over untyped ones.

> For deeply nested `variant` schemas with several different discriminator keys, `variant` will return an issue for the first most likely object schemas on invalid input. The order of the discriminator keys and the presence of a discriminator in the input are taken into account.

## Returns

- `Schema` <Property {...properties.Schema} />

## Examples

The following examples show how `variant` can be used.

### Variant schema

Schema to validate an email, URL or date variant.

```ts
const VariantSchema = v.variant('type', [
  v.object({
    type: v.literal('email'),
    email: v.pipe(v.string(), v.email()),
  }),
  v.object({
    type: v.literal('url'),
    url: v.pipe(v.string(), v.url()),
  }),
  v.object({
    type: v.literal('date'),
    date: v.pipe(v.string(), v.isoDate()),
  }),
]);
```

### Nested variant schema

You can also nest `variant` schemas.

```ts
const NestedVariantSchema = v.variant('type', [
  VariantSchema,
  v.object({
    type: v.literal('color'),
    date: v.pipe(v.string(), v.hexColor()),
  }),
]);
```

### Complex variant schema

You can also use `variant` to validate complex objects with multiple different discriminator keys.

```ts
const ComplexVariantSchema = v.variant('kind', [
  v.variant('type', [
    v.object({
      kind: v.literal('fruit'),
      type: v.literal('apple'),
      item: v.object({ … }),
    }),
    v.object({
      kind: v.literal('fruit'),
      type: v.literal('banana'),
      item: v.object({ … }),
    }),
  ]),
  v.variant('type', [
    v.object({
      kind: v.literal('vegetable'),
      type: v.literal('carrot'),
      item: v.object({ … }),
    }),
    v.object({
      kind: v.literal('vegetable'),
      type: v.literal('tomato'),
      item: v.object({ … }),
    }),
  ]),
]);
```

## Related

The following APIs can be combined with `variant`.

### Schemas

<ApiList items={['object']} />

### Methods

<ApiList
  items={[
    'assert',
    'config',
    'fallback',
    'getDefault',
    'getDefaults',
    'getFallback',
    'getFallbacks',
    'is',
    'message',
    'parse',
    'parser',
    'pipe',
    'safeParse',
    'safeParser',
  ]}
/>

### Actions

<ApiList
  items={[
    'check',
    'brand',
    'description',
    'entries',
    'flavor',
    'maxEntries',
    'metadata',
    'minEntries',
    'notEntries',
    'partialCheck',
    'rawCheck',
    'rawTransform',
    'readonly',
    'title',
    'transform',
  ]}
/>

### Utils

<ApiList items={['entriesFromList', 'isOfKind', 'isOfType']} />
